package com.lleice.web.controller.sys;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lleice.common.annotation.Permission;
import com.lleice.common.annotation.RequestLog;
import com.lleice.service.dto.DepartmentTreeParentDto;
import com.lleice.service.entity.sys.SysDepartment;
import com.lleice.service.qo.sys.DepartmentReq;
import com.lleice.service.qo.sys.DepatmentListPageReq;
import com.lleice.service.service.sys.ISysDepartmentService;

import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 机构相关接口
 * </p>
 *
 * @author lizihao
 * @since 2020-11-18
 */
@Api(tags = {"机构相关接口"})
@RestController
@RequestMapping("/base/sysDepartment")
public class SysDepartmentController {

	@Autowired
	private ISysDepartmentService iSysDepartmentService;
	
	@ApiOperation(value = "新增或修改机构")
	@Permission
	@RequestLog
    @RequestMapping(value = "/addOrUpdate", method = RequestMethod.POST)
	public Boolean add(@Validated @RequestBody DepartmentReq departmentReq) throws Exception{
		iSysDepartmentService.saveOrUpdateUser(departmentReq);
	 return true;
 }
	
	@ApiOperation(value = "删除机构")
	@Permission
	@RequestLog
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
	public Boolean delete(@RequestParam("id") String id) throws Exception{
		SysDepartment role = new SysDepartment();
		role.setId(Long.valueOf(id));
		role.setIsEnable(1);
		iSysDepartmentService.updateById(role);
	 return true;
 }
	
	@ApiOperation(value = "机构信息")
	@Permission
	@RequestLog
    @RequestMapping(value = "/info", method = RequestMethod.POST)
	public SysDepartment info(@RequestParam("id") String id) throws Exception{
	 return iSysDepartmentService.getById(Long.valueOf(id));
 }
	
	@ApiOperation(value = "机构列表")
	@Permission
	@RequestLog
    @RequestMapping(value = "/page", method = RequestMethod.POST)
	public IPage<SysDepartment> page(@RequestBody DepatmentListPageReq pageReq) throws Exception{
		QueryWrapper<SysDepartment> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("is_enable", 0);
		queryWrapper.like(StrUtil.isNotBlank(pageReq.getDepartmentName()),"department_name", pageReq.getDepartmentName());
	 return iSysDepartmentService.page(pageReq.getPage(), queryWrapper);
 }
	
	@ApiOperation(value = "机构树结构")
	@Permission
	@RequestLog
    @RequestMapping(value = "/tree", method = RequestMethod.POST)
	public List<DepartmentTreeParentDto> tree() throws Exception{
	 return iSysDepartmentService.getAllDepartment();
 }
	
	@ApiOperation(value = "机构信息-带用户所在机构")
	@ApiImplicitParams({
	    @ApiImplicitParam(name="userId",value="用户id",required=false,paramType="query")
	})
	@Permission
	@RequestLog
    @RequestMapping(value = "/treeWithUser", method = RequestMethod.POST)
	public List<DepartmentTreeParentDto> treeWithUser(@RequestParam("userId") Long userId) throws Exception{	
	 return iSysDepartmentService.getAllDepartmentWithUser(userId);
 }
	
}
